﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Text;
using blackhouse.JavaScript.JQuery;
using blackhouse.Web.FasterFiles;

namespace blackhouse.Web.Controls {

	[DefaultProperty("MenuElementSelector")]
	public class SuperfishMenu : Control {

		private const string SUPERFISH_MENU_ELEMENT_SELECTOR=".menu";
		private const string SUPERFISH_MENU_HOVER_CLASS="sfHover";
		private const int SUPERFISH_MENU_LEVELS=1;
		private const int SUPERFISH_MENU_DELAY=800;
		private const bool SUPERFISH_MENU_AUTO_ARROWS=true;
		private const bool SUPERFISH_MENU_DROP_SHADOWS=true;
		private const bool SUPERFISH_MENU_DISABLE_HI=false;

		#region Properties

		/// <summary>
		/// Selector wykorzystywany przez jQuery do wybierania elementów: $('{selector}')
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue(SuperfishMenu.SUPERFISH_MENU_ELEMENT_SELECTOR)]
		public string MenuElementSelector { get; set; }

		/// <summary>
		/// The class applied to hovered list items
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue(SUPERFISH_MENU_HOVER_CLASS)]
		public string HoverClass { get; set; }

		/// <summary>
		/// The class you have applied to list items that lead to the current page 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue("")]
		public string PathClass { get; set; }

		/// <summary>
		/// The number of levels of submenus that remain open or are restored using pathClass 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue(SUPERFISH_MENU_LEVELS)]
		public int PathLevels { get; set; }

		/// <summary>
		/// The delay in milliseconds that the mouse can remain outside a submenu without it closing 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue(SUPERFISH_MENU_DELAY)]
		public int Delay { get; set; }

		/// <summary>
		/// If true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue(SUPERFISH_MENU_AUTO_ARROWS)]
		public bool AutoArrows { get; set; }

		/// <summary>
		/// Completely disable drop shadows by setting this to false 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue(SUPERFISH_MENU_DROP_SHADOWS)]
		public bool DropShadows { get; set; }

		/// <summary>
		/// Set to true to disable hoverIntent detection 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue(SUPERFISH_MENU_DISABLE_HI)]
		public bool DisableHoverIntentDetection { get; set; }

		/// <summary>
		/// Javascript callback function fires once Superfish is initialised – 'this' is the containing ul 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue("")]
		public string ClientOnInit { get; set; }

		/// <summary>
		/// Javascript callback function fires just before reveal animation begins – 'this' is the ul about to open 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue("")]
		public string ClientBeforeShow { get; set; }

		/// <summary>
		/// Javascript callback function fires once reveal animation completed – 'this' is the opened ul 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue("")]
		public string ClientOnShow { get; set; }

		/// <summary>
		/// Javascript callback function fires after a sub-menu has closed – 'this' is the ul that just closed 
		/// </summary>
		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue("")]
		public string ClientOnHide { get; set; }

		[Browsable(true)]
		[Category("Menu")]
		[DefaultValue(true)]
		public bool IncludeCss { get; set; }

		#endregion

		#region Constructors

		public SuperfishMenu() {
			this.HoverClass = SUPERFISH_MENU_HOVER_CLASS;
			this.PathClass = "";
			this.PathLevels = SUPERFISH_MENU_LEVELS;
			this.Delay = SUPERFISH_MENU_DELAY;
			this.AutoArrows = SUPERFISH_MENU_AUTO_ARROWS;
			this.DropShadows = SUPERFISH_MENU_DROP_SHADOWS;
			this.DisableHoverIntentDetection = SUPERFISH_MENU_DISABLE_HI;
			this.ClientOnInit = "";
			this.ClientBeforeShow = "";
			this.ClientOnShow = "";
			this.ClientOnHide = "";
			this.MenuElementSelector = SUPERFISH_MENU_ELEMENT_SELECTOR;
			this.IncludeCss = true;
		}

		#endregion

		#region Control Methods

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			List<string> options = new List<string>();
			if(!string.IsNullOrEmpty(this.HoverClass) && this.HoverClass != SUPERFISH_MENU_HOVER_CLASS)
				options.Add("hoverClass: '" + this.HoverClass + "'");
			if(!string.IsNullOrEmpty(this.PathClass))
				options.Add("pathClass: '" + this.PathClass + "'");
			if(this.PathLevels >= 0 && this.PathLevels != SUPERFISH_MENU_LEVELS)
				options.Add("pathLevels: " + this.PathLevels);
			if(this.Delay >= 0 && this.Delay != SUPERFISH_MENU_DELAY)
				options.Add("delay: " + this.Delay);
			if(this.AutoArrows != SUPERFISH_MENU_AUTO_ARROWS)
				options.Add("autoArrows: " + this.AutoArrows.ToString().ToLower());
			if(this.DropShadows != SUPERFISH_MENU_DROP_SHADOWS)
				options.Add("dropShadows: " + this.DropShadows.ToString().ToLower());
			if(this.DisableHoverIntentDetection != SUPERFISH_MENU_DISABLE_HI)
				options.Add("disableHI: " + this.DisableHoverIntentDetection.ToString().ToLower());
			if(!string.IsNullOrEmpty(this.ClientOnInit))
				options.Add("onInit: " + this.ClientOnInit);
			if(!string.IsNullOrEmpty(this.ClientBeforeShow))
				options.Add("onBeforeShow: " + this.ClientBeforeShow);
			if(!string.IsNullOrEmpty(this.ClientOnShow))
				options.Add("onShow: " + this.ClientOnShow);
			if(!string.IsNullOrEmpty(this.ClientOnHide))
				options.Add("onHide: " + this.ClientOnHide);

			StringBuilder builder = new StringBuilder();
			builder.AppendLine("$(document).ready(function() { ");
			builder.AppendLine("\t$('" + this.MenuElementSelector + "').addClass('sf-menu').superfish({ ");
			builder.AppendLine("\t\t" + string.Join(", \r\n\t\t", options.ToArray()));
			builder.AppendLine("\t});");
			builder.AppendLine("});");

			JQueryLibraries jql = new JQueryLibraries();
			FasterFilesProvider ffp = new FasterFilesProvider();

			jql.IncludeFileBasedOnJQuery("blackhouse.Web.Controls.hoverIntent.js,blackhouse.Web.Controls");
			jql.IncludeFileBasedOnJQuery("blackhouse.Web.Controls.superfish.js,blackhouse.Web.Controls");

			ffp.AddAfterScript(builder.ToString());

			if(this.IncludeCss) {
				ffp.AddCssFile(typeof(SuperfishMenu), "blackhouse.Web.Controls.superfish.css");
				ffp.AddCssFile(typeof(SuperfishMenu), "blackhouse.Web.Controls.superfish-vertical.css");
			}
		}

		#endregion

	}
}